using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business;
using Entity;
public partial class EmployeePage_EmployeeManager : System.Web.UI.Page
{
    eEmployee employee=new eEmployee();
    DataTable dt_Type;
    protected void Page_Load(object sender, EventArgs e)
    {
        employee.StoreID = -1;
        dt_Type = bType.getAllType();
        if (!IsPostBack)
        {
            DataTable dt = bEmployee.getAllEmployee();
            gvEmployee.DataSource = dt;
            gvEmployee.DataBind();

            dlType.DataSource = dt_Type;
            dlType.DataTextField = "typeName";
            dlType.DataValueField = "typeID";
            dlType.DataBind();
        }
        btDelete.Attributes.Add("onclick","return confirm('Are you Sure you want to delete this employee')");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = bEmployee.getAllEmployee();
        DataView df = dt.DefaultView;
        df.RowFilter = "typeID='" + dlType.SelectedValue + "'";
        gvEmployee.DataSource = df;
        gvEmployee.DataBind();
    }
    protected void gvEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void gvEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Detail")
        {
            btAdd.Text = "Update";
            txtID.Text = e.CommandArgument.ToString();
            resetForm();
            MultiView1.ActiveViewIndex = 1;
        }
    }
    void resetForm()
    {
        if (btAdd.Text == "Update")
        {
            int emID = int.Parse(txtID.Text);
            employee = bEmployee.getEmployeeByID(emID);
            if (employee != null)
            {
                txtID.Text = employee.employeeID.ToString();
                txtUserName.Text = employee.employeeUserName;
                txtPassword.Text = employee.employeePassword;
                txtConfirm.Text = employee.employeePassword;
                txtFullName.Text = employee.employeeFullName;
                txtAddress.Text = employee.employeeAddress;
                txtEmail.Text = employee.employeeEmail;
                txtPhone.Text = employee.employeePhone;

                dlType2.DataSource = dt_Type;
                dlType2.DataTextField = "typeName";
                dlType2.DataValueField = "typeID";
                dlType2.DataBind();
                
                dlType2.SelectedValue = employee.typeID.ToString();

                if (employee.StoreID != 0)
                {
                    dlStore.Visible = true;
                    lbStore.Visible = true;
                    dlStore.DataSource = bStores.getAllStores();
                    dlStore.DataTextField = "StoreName";
                    dlStore.DataValueField = "StoreID";
                    dlStore.DataBind();
                    dlStore.SelectedValue = employee.StoreID.ToString();
                }

                btDelete.Visible = true;
            }
        }
        else
        {
            txtID.Text = "";
            //txtID.Visible = false;
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtConfirm.Text = "";
            txtFullName.Text = "";
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtPhone.Text = "";

            dlType2.DataSource = dt_Type;
            dlType2.DataTextField = "typeName";
            dlType2.DataValueField = "typeID";
            dlType2.DataBind();
            dlType2.SelectedIndex = 0;

            dlStore.Visible = false;
            lbStore.Visible = false;

            btDelete.Visible = false;
        }
        lbMesss.Text = "";
    }

    protected void btBack_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        int StoreID;
        if (dlType2.SelectedItem.Text == "RetailEmployee" || dlType2.SelectedItem.Text == "Technical")
        {
            StoreID = int.Parse(dlStore.SelectedValue);
        }
        else
        {
            StoreID = -1;
        }
        if (btAdd.Text == "Update")
        {
            int emID = int.Parse(txtID.Text);
            employee = bEmployee.getEmployeeByID(emID);
            employee.employeeUserName = txtUserName.Text;
            if (txtPassword.Text != null)
                employee.employeePassword = txtPassword.Text;
            employee.employeeFullName = txtFullName.Text;
            employee.employeeAddress = txtAddress.Text;
            employee.employeeEmail = txtEmail.Text;
            employee.employeePhone = txtPhone.Text;
            employee.typeID = int.Parse(dlType2.SelectedValue);
            employee.StoreID = StoreID;
            bEmployee.updateEmployee(employee);
            lbMesss.Text = "has updated!";

            DataTable dt = bEmployee.getAllEmployee();
            gvEmployee.DataSource = dt;
            gvEmployee.DataBind();


        }
        else
        {
            
            employee.employeeUserName = txtUserName.Text;
            employee.employeePassword = txtPassword.Text;
            employee.employeeFullName = txtFullName.Text;
            employee.employeeAddress = txtAddress.Text;
            employee.employeeEmail = txtEmail.Text;
            employee.employeePhone = txtPhone.Text;
            employee.typeID = int.Parse(dlType2.SelectedValue);
            bEmployee.insertEmployee(employee);
            lbMesss.Text = "has Added!";

            DataTable dt = bEmployee.getAllEmployee();
            gvEmployee.DataSource = dt;
            gvEmployee.DataBind();
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void btReset_Click(object sender, EventArgs e)
    {
        if (btAdd.Text == "Update")
        {
            int emID = int.Parse(txtID.Text);
            employee = bEmployee.getEmployeeByID(emID);
        }
        resetForm();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        btAdd.Text = "Add New";
        resetForm();
        MultiView1.ActiveViewIndex = 1;
    }
    protected void btDelete_Click(object sender, EventArgs e)
    {
        int emID = int.Parse(txtID.Text);
        bEmployee.deleteEmployee(emID);

        DataTable dt = bEmployee.getAllEmployee();
        gvEmployee.DataSource = dt;
        gvEmployee.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void dlType2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlType2.SelectedItem.Text == "RetailEmployee" || dlType2.SelectedItem.Text == "Technical")
        {
            dlStore.Visible = true;
            lbStore.Visible = true;
            if (dlStore.SelectedItem == null)
            {
                // Response.Write("resd");
                dlStore.DataSource = bStores.getAllStores();
                dlStore.DataTextField = "StoreName";
                dlStore.DataValueField = "StoreID";
                dlStore.DataBind();           
            }
           
        }
        else
        {
            dlStore.Visible = false;
            lbStore.Visible =false;
        }
    }
}
